package com.sxau.software_educational_system_backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxau.software_educational_system_backend.annotation.AdminOneOrTwo;
import com.sxau.software_educational_system_backend.annotation.AdminOnlyOne;
import com.sxau.software_educational_system_backend.annotation.AdminOnlyTwo;
import com.sxau.software_educational_system_backend.common.BaseResponse;
import com.sxau.software_educational_system_backend.common.PageResult;
import com.sxau.software_educational_system_backend.common.ResultUtils;
import com.sxau.software_educational_system_backend.domain.entity.Teacher;
import com.sxau.software_educational_system_backend.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sxau.software_educational_system_backend.utils.PasswordUtils.hashPassword;

@RestController
@RequestMapping("/teachers")
@RequiredArgsConstructor
@Api(tags = "教师信息管理接口")
public class TeacherController {
    private final TeacherService teacherService;

    @GetMapping
    @ApiOperation("分页查询教师信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10")
    })
    public BaseResponse<PageResult<Teacher>> getPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Teacher> teacherPage = new Page<>(page, pageSize);
        teacherService.page(teacherPage);
        return ResultUtils.success(new PageResult<>(
                teacherPage.getTotal(),    // long → Long (自动装箱)
                teacherPage.getPages(),    // long → Long (自动装箱)
                teacherPage.getRecords()   // List<Teacher> → List<T> (T=Teacher)
        ));
    }

    @PostMapping("/yz")
    @ApiOperation("新增教师信息")
    @AdminOneOrTwo
    public BaseResponse<Teacher> create(@RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return ResultUtils.success(teacher);
    }

    @DeleteMapping("/yz/{id}")
    @ApiOperation("删除教师信息")
    @AdminOneOrTwo
    public BaseResponse<Void> delete(@PathVariable Long id) {
        teacherService.removeById(id);
        return ResultUtils.success();
    }

    @PutMapping("/yz/{id}")
    @ApiOperation("更新教师信息")
    @AdminOneOrTwo
    public BaseResponse<Teacher> update(@PathVariable Integer id ,@RequestBody Teacher teacher) {
        teacher.setId(id);
        teacherService.updateById(teacher);
        return ResultUtils.success(teacherService.getById(id));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询教师详情")
    public BaseResponse<Teacher> getById(@PathVariable Long id) {
        return ResultUtils.success(teacherService.getById(id));
    }

    @GetMapping("/getAll")
    @ApiOperation("获取所有教师列表")
    public BaseResponse<List<Teacher>> getAll() {
        return ResultUtils.success(teacherService.list());
    }
}
